package com.bluefox.hospital.controller;

import com.bluefox.common.web.dto.result.ListResultDTO;
import com.bluefox.common.web.dto.result.PageResultDTO;
import com.bluefox.common.web.dto.result.ResultDTO;
import com.bluefox.hospital.convertor.DepartmentConvertor;
import com.bluefox.hospital.dto.DepartmentDTO;
import com.bluefox.hospital.model.Department;
import com.bluefox.hospital.repository.DepartmentRepository;
import com.bluefox.hospital.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Department的管理接口
 *
 * @author
 */
@Slf4j
@RestController
@RequestMapping("/w/departments")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private DepartmentRepository departmentRepository;
    @Autowired
    private DepartmentConvertor departmentConvertor;

    /**
     * 获取分页数据
     *
     * @param pageable 分页+排序参数
     * @return 分页数据
     */
    @RequestMapping(value = "/s", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResultDTO<DepartmentDTO> search(final Pageable pageable) {
        final Page<Department> models = this.departmentRepository.findAll(pageable);
        return this.departmentConvertor.toResultDTO(models);
    }
    /**
     * 获取全部部门
     * @return
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ListResultDTO<DepartmentDTO> findAll() {
        List<Department> all = this.departmentRepository.findAll();
        return this.departmentConvertor.toResultDTO(all);
    }
    /**
     * 取得详细数据
     *
     * @param id 资源ID
     * @return 资源详细
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<DepartmentDTO> get(@PathVariable final Long id) {
        final Department model = this.departmentService.get(id);
        return this.departmentConvertor.toResultDTO(model);
    }
    @RequestMapping(value = "/findByName", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<DepartmentDTO> findByName(@RequestParam String name) {
        final Department model = this.departmentService.get(name);
        return this.departmentConvertor.toResultDTO(model);
    }
    /**
     * 新建操作
     *
     * @param departmentDTO 新建资源的DTO
     * @return 新建资源
     */

    @RequestMapping(method = RequestMethod.POST)
    public ResultDTO<DepartmentDTO> create(@RequestBody @Valid final DepartmentDTO departmentDTO) {
        final Department model = this.departmentConvertor.toModel(departmentDTO);
        this.departmentService.create(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was created.", Department.class.getSimpleName(), model.getId());
        }
        return this.departmentConvertor.toResultDTO(model);
    }

    /**
     * 更新操作
     *
     * @param id            更新资源的ID
     * @param departmentDTO 更新资源的DTO
     * @return 更新后资源
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<DepartmentDTO> update(@PathVariable final Long id, @RequestBody @Valid final DepartmentDTO departmentDTO) {
        departmentDTO.setId(id);
        final Department model = this.departmentConvertor.toModel(departmentDTO);
        this.departmentService.update(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", Department.class.getSimpleName(), model.getId());
        }
        return this.departmentConvertor.toResultDTO(model);
    }

    /**
     * 删除操作
     *
     * @param Id 资源ID
     * @return 操作结果
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.departmentService.delete(id);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was deleted.", Department.class.getSimpleName(), id);
        }
        return ResultDTO.success();
    }

    /**
     * 根据父id查询列表
     * @param parentId
     * @return
     */
 /*   @RequestMapping(value = "/findByParentId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ListResultDTO<DepartmentDTO> findByParentId(@RequestParam Long parentId) {
        List<Department> list = this.departmentRepository.findByParentIdOrderByDepartmentOrderDesc(parentId);
        return this.departmentConvertor.toResultDTO(list);
    }*/

    /**
     * 获取树形菜单结构
     *
     * @param parentId
     * @return
     */
   /* @RequestMapping(value = "/departmentMenus", method = RequestMethod.POST)
    public Map<Object, Object> getDepartmentMenus(@RequestParam(name = "parentId", required = true) Long parentId) {
        List<DepartmentDTO> departments = this.departmentConvertor.toListDTO(this.departmentRepository.findAll());
        List<DepartmentDTO> list = new ArrayList<DepartmentDTO>();
        for (int i = 0; i < departments.size(); i++) {

            if (departments.get(i).getParentId().equals(parentId)) {
                if (this.departmentConvertor.judgeEle(departments, departments.get(i).getId())) {
                    departments.get(i).setChildren(this.departmentConvertor.recombineTree(departments, departments.get(i).getId()));
                }
                list.add(departments.get(i));
            }
        }
        HashMap<Object, Object> map = new HashMap<>();
        map.put("status", "success");
        map.put("data", list);
        return map;
    }*/

    /**
     * 获取全部的Departments
     * @return
     */
    @RequestMapping(value = "/getAllDept", method = RequestMethod.POST)
    public List<DepartmentDTO> getAllDept(){
        List<Department> models = this.departmentRepository.findAll();
        return this.departmentConvertor.toListDTO(models);
    }

    /**
     * 获取重点部门
     */
    @RequestMapping(value = "/getKeyDepartments", method = RequestMethod.GET)
    public List<DepartmentDTO> getKeyDepartments(){
        List<Department> models = this.departmentService.findByIsKeyDepartment(1);
        return this.departmentConvertor.toListDTO(models);
    }
}
